package com.fmdj.bff.driver.service.impl;
import cn.hutool.core.map.MapUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.fmdj.bff.driver.controller.form.*;
import com.fmdj.bff.driver.feign.CstServiceApi;
import com.fmdj.bff.driver.feign.DrServiceApi;
import com.fmdj.bff.driver.feign.OdrServiceApi;
import com.fmdj.bff.driver.service.DriverService;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.CosUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

@Service
@Slf4j
public class DriverServiceImpl implements DriverService {

    //用这个接口访问子系统
    @Autowired
    private DrServiceApi drServiceApi;

    @Autowired
    private OdrServiceApi odrServiceApi;

    @Autowired
    private CstServiceApi cstServiceApi;
    @Autowired
    private CosUtil cosUtil;
    @Transactional
    @LcnTransaction
    @Override
    public Long registerNewDriver(RegisterDriverForm form) {
        //调用子系统
        CommonResult commonResult = drServiceApi.registerDriver(form);
        Object driverId = commonResult.get("userId");  //这里一定要注意获取的参数名是userId 从子系统返回的参数是CommonResult.RETURN_USER_ID所以也要根据userId取出放入的driverId
        //返回给前端
        return Long.parseLong(driverId.toString());   //返回值一定要是Long 否则sa-token会报错
    }
    @Transactional
    @LcnTransaction
    @Override
    public int updateDriverAuth(UpdateDriverAuthForm form) {
        CommonResult commonResult = drServiceApi.updateDriverAuth(form);
        return Integer.parseInt(commonResult.get(CommonResult.RETURN_ROW).toString());
    }

    //查询不需要事务管理以及分布式事务管理
    @Override
    public HashMap login(LoginForm form) {
        //调用子系统
        CommonResult login = drServiceApi.login(form);
        //将子系统返回的参数取出来,再返回给controller层
        return (HashMap)login.get(CommonResult.RETURN_RESULT);
    }

    @Override
    public HashMap selectDriverInfo(SelectDriverInfoForm form) {
        CommonResult commonResult = drServiceApi.selectDriverInfo(form);
        return (HashMap)commonResult.get(CommonResult.RETURN_RESULT);
    }

    @Override
    public HashMap selectWorkbenchData(Long driverId) {

        //查询司机当日的订单数据 duration income orders
        SelectDriverTodayBusinessDataForm form1 =  new SelectDriverTodayBusinessDataForm();
        form1.setDriverId(driverId);
        CommonResult commonResult1= odrServiceApi.selectDriverTodayBusinessData(form1);
        HashMap result1 = (HashMap)commonResult1.get(CommonResult.RETURN_RESULT);


        //查询司机的设置信息 setting: {.......}
        SelectDriverSettingsForm form2 = new SelectDriverSettingsForm();
        form2.setDriverId(driverId);
        CommonResult commonResult2 = drServiceApi.selectDriverSettings(form2);
        HashMap result2 = (HashMap)commonResult2.get(CommonResult.RETURN_RESULT);

        //构造结果集
        HashMap result = new HashMap();
        result.put("business", result1);
        result.put("setting", result2);

        return result;
    }

    @Override
    public HashMap selectDriverInfoById(SelectDriverAuthForm form) {
        CommonResult commonResult = drServiceApi.selectDriverInfoById(form);
        HashMap result = (HashMap) commonResult.get(CommonResult.RETURN_RESULT);

        //将返回的result中的关于身份证的url和驾驶证的url换成完整的url
        //身份证和驾驶证的url都是保存在数据库中的相对路径，需要转换成完整的url
        String idcardFront = MapUtil.getStr(result, "idcardFront");
        String idcardBack = MapUtil.getStr(result, "idcardBack");
        String idcardHolding = MapUtil.getStr(result, "idcardHolding");
        String drcardFront = MapUtil.getStr(result, "drcardFront");
        String drcardBack = MapUtil.getStr(result, "drcardBack");
        String drcardHolding = MapUtil.getStr(result, "drcardHolding");

        //获取完整的url
        String idcardFrontUrl = idcardFront.length() > 0 ? cosUtil.getPrivateFileUrl(idcardFront) : "";
        String idcardBackUrl = idcardBack.length() > 0 ? cosUtil.getPrivateFileUrl(idcardBack) : "";
        String idcardHoldingUrl = idcardHolding.length() > 0 ? cosUtil.getPrivateFileUrl(idcardHolding) : "";
        String drcardFrontUrl = drcardFront.length() > 0 ? cosUtil.getPrivateFileUrl(drcardFront) : "";
        String drcardBackUrl = drcardBack.length() > 0 ? cosUtil.getPrivateFileUrl(drcardBack) : "";
        String drcardHoldingUrl = drcardHolding.length() > 0 ? cosUtil.getPrivateFileUrl(drcardHolding) : "";

        //将完整的url放入result中
        result.put("idcardFrontUrl", idcardFrontUrl);
        result.put("idcardBackUrl", idcardBackUrl);
        result.put("idcardHoldingUrl", idcardHoldingUrl);
        result.put("drcardFrontUrl", drcardFrontUrl);
        result.put("drcardBackUrl", drcardBackUrl);
        result.put("drcardHoldingUrl", drcardHoldingUrl);

        return result;
    }

}
